package com.lhf.novel.util;

import lombok.Data;

/**
 * <p>返回结果封装</p>
 *
 * @author zy 刘会发
 * @since 2020/3/12
 */
@Data
public class Result<T> {
    private int code;
    private T data;
    private String mes;
    private String token;

    public Result(int code, T data, String mes) {
        this.code = code;
        this.data = data;
        this.mes = mes;
    }

    /**
     * 请求成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(ExceptionCode.SUCCESS.getCode(), data, ExceptionCode.SUCCESS.getMes());
    }

    /**
     * 请求失败
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failed() {
        return new Result<T>(ExceptionCode.FAILED.getCode(), null, ExceptionCode.FAILED.getMes());
    }

    /**
     * 其他错误类型
     *
     * @param code
     * @param data
     * @param mes
     * @param <T>
     * @return
     */
    public static <T> Result<T> other(int code, T data, String mes) {
        return new Result<T>(code, data, mes);
    }
}
